<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()


const breadcrumbs = ref([])


const getBreadcrumbs = () => {
  const matched = route.matched.filter(item => item.meta && item.meta.title)
  const first = matched[0]
  
  // 如果不是首页，添加首页
  if (first && first.path !== '/dashboard') {
    breadcrumbs.value = [
      {
        path: '/dashboard',
        meta: { title: '首页' }
      }
    ]
  } else {
    breadcrumbs.value = []
  }
  
  // 添加其他路径
  breadcrumbs.value = [...breadcrumbs.value, ...matched]
}

// 监听路由变化
watch(
  () => route.path,
  () => {
    getBreadcrumbs()
  },
  { immediate: true }
)

// 点击面包屑导航
const handleLink = (item) => {
  router.push(item.path)
}
</script>

<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item 
      v-for="(item, index) in breadcrumbs" 
      :key="index"
      :to="index < breadcrumbs.length - 1 ? { path: item.path } : ''"
    >
      {{ item.meta.title }}
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<style scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 14px;
}
</style> 